<template>
  <div id="homeheader">
    <div style="display: flex;">
      <div class="logo">
        <img src="../../../assets/img/logo.png" />
      </div>
      <div class="title">
        <p>社区养老服务管理系统</p>
      </div>
    </div>
    <div style="display: flex;align-items: center;">
      <el-dropdown @command="handleCommand">
        <!-- http://localhost:18081/avatar/pangke.jpg -->
        <!-- <el-avatar v-if="avatar == null" src="https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png" />
        <el-avatar v-if="avatar !== null" :src="imgUrl" /> -->
        <el-button style="margin-right: 100px;">
          你好，{{ username }}<el-icon class="el-icon--right"><arrow-down /></el-icon>
        </el-button>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item command="IndexView" v-if="roleId == 1">首页</el-dropdown-item>
            <!-- <el-dropdown-item command="UploadAvatarCopy">更换头像</el-dropdown-item> -->
            <!-- <el-dropdown-item command="PersonalInfo">账号设置</el-dropdown-item> -->
            <!-- <el-dropdown-item command="login">切换账号</el-dropdown-item> -->
            <el-dropdown-item command="login">退出登录</el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>

      <!-- <el-button :icon="SwitchButton" @click="toLogin">退出</el-button> -->
    </div>
  </div>
</template>

<script setup>
import { SwitchButton, ArrowDown } from '@element-plus/icons-vue'
import { ref } from 'vue';
import { useRouter } from 'vue-router';

// import { useUserStore } from '../../stores/user'
// import { storeToRefs } from 'pinia'
// const userStore = useUserStore()

const router = useRouter()
const handleCommand = (command) => {
  if (command === 'login') {
    // 清除登录信息
    localStorage.removeItem('user')
    localStorage.removeItem('token')
    localStorage.removeItem('roleId')
    localStorage.removeItem('id')
  }
  router.push(`/${command}`)
}

const roleId = localStorage.getItem('roleId')

const username = ref('')
try {
  const userStr = localStorage.getItem('user')
  if (userStr) {
    const userData = JSON.parse(userStr)
    username.value = userData.username
  } else {
    // 如果用户未登录，跳转到登录页
    router.push('/login')
  }
} catch (error) {
  console.error('Error parsing user data:', error)
  router.push('/login')
}
console.log(username.value);

// const avatar = sessionStorage.getItem('avatar')
// console.log(avatar);
// const imgUrl = sessionStorage.getItem('imgUrl')
// console.log(imgUrl);
// const url = ref("http://localhost:18081/avatar/" + username.value + ".jpg")
// console.log(url.value)
// const { imageUrl } = storeToRefs(userStore);
// console.log(imageUrl);
// const initAvatar = () => {
//   userStore.initAvatar()
// }
// initAvatar()

</script>

<style lang="scss" scoped>
#homeheader {
  display: flex;
  justify-content: space-between;

  .logo {
    img {
      height: 68px;
    }
  }

  .title {
    p {
      line-height: 68px;
      font-family: '微软雅黑', Courier, monospace;
      font-size: 1.875rem;
      color: rgb(0, 0, 0);
    }
  }
}
</style>